<template>
	<view>
		<view style="display: flex;flex-direction: column; margin: 50rpx auto 20rpx auto;width: 80%;background-color: white;border-radius: 20rpx;box-shadow: 1px 1px 5px #c2c2c2;padding: 30rpx;">
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;">
				<view style="display: flex;width: 180rpx;">
					手机号
				</view>
				<input style="width: 570rpx; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;" type="number" v-model="tel" placeholder="请输入手机号">
				<!-- <uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="tel" type="number"
					placeholder="请输入手机号" /> -->
			</view>
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				<view style="display: flex;width: 180rpx;">
					验证码
				</view>	
				<input style="width: 350rpx; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;" type="number" v-model="yanzhengma" placeholder="请输入验证码">
				<!-- <uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;margin-right: 20rpx; " v-model="name" type="text"
					placeholder="请输入验证码" /> -->
				<button style="background-color: #247DFD;color: white;padding: 5rpx 10rpx;width: 220rpx;" size='mini' @click="duanxin">{{smstext}}</button>
			</view>	
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				<view style="display: flex;width: 180rpx;">
					密码
				</view>
				<input style="width: 570rpx; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;" type="password" v-model="mima" placeholder="请输入密码">
				<!-- <uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="mima" type="password"
					placeholder="请输入密码" /> -->
			</view>
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				<view style="display: flex;width: 180rpx;">
					重复密码
				</view>
				<input style="width: 570rpx; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;" type="password" v-model="remima" placeholder="再输入一次密码">
				<!-- <uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="remima" type="password"
					placeholder="再输入一次密码" /> -->
			</view>	
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				<view style="display: flex;width: 180rpx;">
					推荐人
				</view>
				<input style="width: 570rpx; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;"  :disabled="iskeyong" type="number" v-model="shangjitel" placeholder="请输入推荐人手机号">
				<!-- <uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="remima" type="password"
					placeholder="再输入一次密码" /> -->
			</view>	
		</view>
		<view style="display: flex;justify-content: center;margin-top: 30rpx;"  @click="xiazai()">已有账号，下载APP</view>
		<view @click="zhuce" style="display: flex;width: 40%;margin: 80rpx auto 50rpx auto;background-color: blue;border-radius: 20rpx;color: white;font-weight: bold;padding: 20rpx;align-items: center;justify-content: center;">
			注册
		</view>
	</view>
</template>

<script>
	import md5 from '@/util/md5';
	import getDateTime from '@/common/getdateTime.js';
	export default {
		data() {
			return {
				// userinfo:[],
				code:0,//短信验证码随机数
				smstext:'获取验证码',
				yanzhengma:'',
				tel:'',
				tel1:'',//识别验证码获取手机号和最终保存手机号是否为同一个
				shangjitel:'',//记录上级的电话
				shangjiid:'',//记录上级的ID
				mima:'',
				remima:'',
				iskeyong:false,
			}
		},
		onLoad(options) {
			const plainPassword = '123456';
			const hashedPassword = this.hashPassword(plainPassword);
			console.log(hashedPassword);
			this.shangjitel=options.tel
			this.shangjiid=options.id
			// console.log(this.shangjiid,this.shangjitel)
			if(this.shangjitel){
				this.iskeyong=true
			}
		},
		methods: {
				
				hashPassword(password, salt = '') {
				  const iterations = 20; // 迭代次数
				  const keyLength = 42; // 密钥长度
				  const crypto = require('crypto');
				  // 生成HMAC-SHA256哈希
				  const derivedKey = crypto.pbkdf2Sync(
				    password,
				    salt,
				    iterations,
				    keyLength,
				    'sha256'
				  );
				  
				  return `$UNI_ID_HMAC_SHA256$${iterations}$${derivedKey.toString('hex')}`;
				},
			xiazai(){
				window.location.href='http://app.dfckpay.com/daomengzhijia'
			},
			zhuce(){
				if(this.mima!==this.remima){
					this.mima=this.remima=''
					return uni.showToast({
						title: '两次密码输入不一致，请重新输入',
						icon: 'none',
						mask: true
					})
				}
				if(this.mima.length<=5){
					this.mima=this.remima=''
					return uni.showToast({
						title: '密码位数应大于等于6位！',
						icon: 'none',
						mask: true
					})
				}
				if(Number(this.yanzhengma)!==Number(this.code)){
					this.yanzhengma=''
					return uni.showToast({
						title: '验证码错误！',
						icon: 'none',
						mask: true
					})
				}
				if(this.tel1!==this.tel){
					this.yanzhengma=''
					return uni.showToast({
						title: '请重新获取验证码！',
						icon: 'none',
						mask: true
					})
				}
				uniCloud.callFunction({
					name:"yonghu",
					data:{
						leixing:0,//为0时，注册新用户
						mima:md5(this.mima),
						tel:this.tel,	
						shangjiid:this.shangjiid,
						time:getDateTime.dateTimeStr('y-m-d h:i:s')
					}
				}).then(res=>{
					uni.showToast({
						title: '注册成功！',
						icon: 'none',
						duration:1000
					})
					this.xiazai()
				}).catch((err) => {
					console.log(err)
					uni.showToast({
						title: '注册失败，请检查本手机号是否已注册！',
						icon: 'none',
						duration:3000
					})
				})
			},
			duanxin(){
				//60秒内，不执行第二次
				if (this.smstext !== '获取验证码') {
					return;
				}
				// 检查手机号是否正确
				let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(this.tel)) {
					return uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						mask: true
					})
				}
				//上面两个都没问题，执行发送短信
				this.tel1=this.tel
				let code=Math.floor(Math.random() *9000) + 1000
				this.code=code
				console.log("this.code",this.code)
				uniCloud.callFunction({
					name:"duanxin",
					data:{
						phone:this.tel,
						code:this.code,
					}
				}).then(res=>{
					console.log("123",res)
					let num = 60;
					this.smstext = num;
					let timer = setInterval(() => {
						num -= 1;
						this.smstext = num;
						if (num == 1) {
							this.smstext = '获取验证码'
							clearInterval(timer)
						}
					}, 1000);
					uni.showToast({
						title: '发布完成',
						icon: 'none',
						duration: 3000
					})
				})
			},
		}
	}
</script>

<style>

</style>
